<template>
  <el-form :model="passForm" status-icon :rules="rules" ref="passForm" label-width="100px" class="passContainer">
    <h3 style="text-align: center">重置密码</h3>
    <el-form-item label="请输入手机号" prop="phonenum">
      <el-input type="text" v-model="passForm.phone" placeholder="请输入手机号"></el-input>
      <el-button style="margin-top: 5px" type="primary">发送验证码</el-button>
    </el-form-item>
    <el-form-item label="输入验证码" prop="vcode">
      <el-input type="password" v-model="passForm.vcode" autocomplete="off" placeholder="请输入验证码"></el-input>
    </el-form-item>
    <el-form-item label="请输入新密码" prop="newpass">
      <el-input type="password" v-model="passForm.newpass" autocomplete="off" placeholder="请输入新密码"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkpass">
      <el-input type="password" v-model="passForm.checkpass" autocomplete="off" placeholder="请再输入一遍新密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('passForm')">确认重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "ForgetPass",
  data(){
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      }
      let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
      if(!phoneReg.test(value)){
        callback(new Error('请输入正确格式手机号'));
      }
      callback();
    };
    var validateCode = (rule,value, callback) =>{
      if(value === ''){
        callback(new Error('请输入验证码'));
      }else{
        callback();
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.passForm.checkpass !== '') {
          this.$refs.passForm.validateField('checkpass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.passForm.newpass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      passForm: {
        phone: '',
        vcode: '',
        newpass: '',
        checkpass: ''
      },
      rules:{
        phonenum: [
          { validator: checkPhone, trigger: 'blur' }
        ],
        vcode:[
          {validator: validateCode,trigger: 'blur'}
        ],
        newpass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkpass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.passForm.validate((valid) => {
        if (valid) {
          let data = {phone:this.passForm.phonenum, newpass:this.passForm.newpass}
          //发送put修改请求，并传递手机号和新密码回去
          this.putRequest("/changePass",data).then(res=>{});
          this.$message({
            message: '重置密码成功',
            type: 'success'
          });
          this.$router.replace('/');
        } else {
          this.$message.error('请按要求输入数据');
          return false;
        }
      });
    }}
}
</script>

<style>
.passContainer{
  border-radius: 15px;
  background-clip: padding-box;
  margin: 100px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: aliceblue;
  border: 1px solid #aaaaaa;
  box-shadow: 0 0 25px #cacaca;
}
</style>
